package em.job;

import base.jfinal.anatation.TimerBind;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import em.util.CommonTools;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 每一小时调用该服务
 * GPS坐标转百度地图坐标
 * @author Administrator
 *
 */
@TimerBind(ClassSpace="em.job.BMapConvertor",Cron="0 0 0/1 ? * *",Enable=true)
public class BMapConvertor implements Job {
	private Logger log4j = Logger.getLogger(BMapConvertor.class);
	//取得详细地址
	String bak="http://restapi.amap.com/v3/geocode/regeo?location=%1$s&key=e63c171500f31b870f92e6a5a0a994db&s=rsv3&radius=1000&extensions=base&callback=json";
	//取得经纬度坐标
	String restak="http://restapi.amap.com/v3/assistant/coordinate/convert?locations=%1$s&coordsys=gps&output=json&key=0ddc7d7761e8f25d3aea22e0181d642a";
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		try{
			Double Longitude,Latitude;
			String strEntity,strSql;
			JSONObject jsonObject;
			Map<String, Object> map=new HashMap<String, Object>();
			List<Record> lstData=Db.find(CommonTools.getInstance().getSql("eqpt.getEqptMapList"));
			for (Record record : lstData) {
				try {
					if (record.getInt("LongitudeRaw") != 0 && record.getInt("LatitudeRaw") != 0) {
						Longitude = record.getInt("LongitudeRaw") / 1000000.0;
						Latitude = record.getInt("LatitudeRaw") / 1000000.0;
						map.clear();
						strEntity = HttpKit.get(String.format(restak, Longitude + "," + Latitude));
						strEntity = JSONObject.parseObject(strEntity).getString("locations");//取得经纬度
						if (StrKit.notBlank(strEntity) && strEntity.contains(",")) {
							Longitude = Double.parseDouble(strEntity.split(",")[0]);
							Latitude = Double.parseDouble(strEntity.split(",")[1]);
							map.put("Longitude", Longitude);
							map.put("Latitude", Latitude);
							map.put("GPS", "1");
							strEntity = HttpKit.get(String.format(bak, strEntity));//通过http取得详细地址
							strEntity = strEntity.replace("json(", "");
							strEntity = strEntity.substring(0, strEntity.lastIndexOf(")"));
							strEntity = JSONObject.parseObject(strEntity).getString("regeocode");
							jsonObject = JSON.parseObject(strEntity);
							map.put("Province", jsonObject.getJSONObject("addressComponent").getString("province"));
							if (!"[]".equals(jsonObject.getJSONObject("addressComponent").getString("city")))
								map.put("City", jsonObject.getJSONObject("addressComponent").getString("city"));
							else
								map.put("City", jsonObject.getJSONObject("addressComponent").getString("province"));
							if (!"[]".equals(jsonObject.getJSONObject("addressComponent").getString("district")))
								map.put("District", jsonObject.getJSONObject("addressComponent").getString("district"));
							if (!"[]".equals(jsonObject.getJSONObject("addressComponent").getString("township")))
								map.put("Township", jsonObject.getJSONObject("addressComponent").getString("township"));
							map.put("EqptId", record.getInt("EqptId"));
							map.put("Carg", record.getStr("Carg"));
							map.put("Curaddress", jsonObject.getString("formatted_address") + "附近");
							strSql = CommonTools.getInstance().getSql("eqpt.updateEqptMap", map);
							Db.update(strSql);
						}
					}
				}catch (Exception e){
					log4j.error("设备Id:"+record.getInt("EqptId")+"-"+record.getStr("Carg")+"出错!错误详细:"+e.getMessage());
				}
			}
			log4j.info("更新完成!");
		}catch(Exception ex){
			log4j.error("原始GPS坐标转为高德地图坐标出错！错误详情:{" + ex.getMessage() + "}");
		}
	}
}
